<template lang="">
  <div class="box">
    <el-card shadow="hover" class="form">
      <!--  -->
      <template #header>
        <h3>XXX管理系统</h3>
      </template>
      <!--  -->
      <!-- 内容 -- form  -->
      <!--  -->
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model.number="ruleForm.username" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="pass">
          <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!--  -->
    </el-card>
  </div>
</template>
<script>
// 在 src/api/request.js 中二次封装 axios 后引入
// import http from '@/api/request.js'
import axios from 'axios'
// import { a1 } from '@/api/ruse.js'
import { ElMessage } from 'element-plus'

export default {
  data() {
    return {
      ruleForm: {
        pass: '123456',
        username: 'admin'
      },
      // 定义表单的检验规则
      rules: {
        username: [
          {
            // 必须传
            required: true,
            // message 提示信息
            message: '用户名只能是字母+字母数字下划线*4',
            // 正则表达式：开头只能是字母，至少四个字母数字下划线
            pattern: /^[a-zA-Z]\w{4,}$/
          }
        ],
        pass: [
          {
            // 必须传
            required: true,
            // message 提示信息
            message: '密码不能为空'
          }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      //   console.log(this.$refs.ruleForm) // 获取表单组件

      // 调用表单组件的自己方法，获取表单内部验证结果
      this.$refs.ruleForm.validate((r) => {
        // console.log(r)
        if (r) {
          // 通过验证
          // 发起登录请求
          // npm i axios -- 终端安装axios
          // 对象方式
          /*  http({
              method: 'post', // 请求方式
              url: '/admin/admin/login',
              // 请求体
              data: { adminname: this.ruleForm.username, password: this.ruleForm.pass }
            }).then((r) => {
              console.log(r.data)
            }) */
          //---------封装--------------------------------------------------------------------
          //   a1(this.ruleForm.username, this.ruleForm.pass).then((r) => {
          //     console.log(r.data)
          //   })
          //---------------------------------------------------------------------------------
          let loginAPI = 'http://localhost:3001/admin/admin/login'
          let obj = { adminname: this.ruleForm.username, password: this.ruleForm.pass }
          axios.post(loginAPI, obj).then((r) => {
            console.log(r.data)
            if (r.data.code == 200) {
              // 请求成功以后 存入本地数据
              localStorage.setItem('token', r.data.data.token)
              localStorage.setItem('adminname', r.data.data.adminname)
              // 成功提示
              ElMessage({
                showClose: true,
                message: r.data.message,
                type: 'success'
              })

              // 登录成功，跳转首页
              this.$router.push('/home')
            } else {
              // 请求失败
              ElMessage({
                showClose: true,
                message: '请检查输入内容',
                type: 'error'
              })
            }
          })
        } else {
          // 没有通过验证
        }
      })
    },
    resetForm() {
      console.log(111111)
    }
  }
}
</script>
<style lang="css">
.box {
  height: 100vh;
  background-image: url('@/assets/image/bg.jpg');
  /* background-repeat: no-repeat;
  background-size: 100%; */
  display: flex;
}

.form {
  width: 40%;
  margin: auto;
}
</style>
